package com.dz.Incubator.controller;

import com.dz.Incubator.config.Pagination;
import com.dz.Incubator.config.ReturnCode;
import com.dz.Incubator.entity.BannerEntity;
import com.dz.Incubator.entity.InvokeResult;
import com.dz.Incubator.service.BannerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/api/a/zx/banner")
@Slf4j
public class BannerController {


	@Autowired
	private BannerService bannerBusinessService;

	/**
	 * 分页查询 banner 对象
	 *
	 */
	@RequestMapping(value = "/getPageList", method = RequestMethod.POST)
    @ResponseBody
	public InvokeResult<Pagination> getPageList(@RequestBody Pagination pagination) {
		InvokeResult invokeResult = null;
		try {
			if (null == pagination) {
				return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
			}
			Pagination pageInfo = bannerBusinessService.findByPagination(pagination);
			if (null != pageInfo && null != pageInfo.getList() && pageInfo.getList().size() > 0) {
				invokeResult = InvokeResult.success(pageInfo);
			} else {
				invokeResult = InvokeResult.pass_no_data();
			}
		} catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
		return invokeResult;
	}

	/**
	 * 获取单个BannerEntity 对象
	 *
	 * @param id Id
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	public InvokeResult<BannerEntity> get(@PathVariable Long id) {
		InvokeResult invokeResult = null;
		try {
			if (null != id) {
				BannerEntity bannerEntity = bannerBusinessService.get(id);
				if (null == bannerEntity) {
					invokeResult = InvokeResult.pass_no_data();
				}
				invokeResult = InvokeResult.success(bannerEntity);
			} else {
				return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
			}
		} catch (Exception e) {
			invokeResult = InvokeResult.error();
			log.error(e.getMessage(), e);
		}
		return invokeResult;
    }

	/**
	 * 删除单个bannerEntity 对象
	 *
	 * @param id 删除数据 ID
	 */
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	@ResponseBody
	public InvokeResult delete(@PathVariable Long id) {
		InvokeResult invokeResult = null;
		try {
			if (null != id) {
				bannerBusinessService.delete(id);
				invokeResult = InvokeResult.success();
			} else {
				return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
			}
		} catch (Exception e) {
			invokeResult = InvokeResult.error();
			log.error(e.getMessage(), e);
		}
		return invokeResult;
    }
    
	/**
	 * 集合查找 bannerEntity 对象
	 *
	 */
	@RequestMapping(value = "/find", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public InvokeResult<List<BannerEntity>> find(@RequestParam("ids") List<Long> ids) {
		InvokeResult invokeResult = null;
		try {
			if (null != ids && ids.size() > 0) {
					Long[] skuIds = ids.toArray(new Long[ids.size()]);
					List<BannerEntity> bannerEntitys = bannerBusinessService.find(skuIds);
				if (null != bannerEntitys && bannerEntitys.size() > 0) {
					invokeResult = InvokeResult.success(bannerEntitys);
				} else {					
					invokeResult = InvokeResult.pass_no_data();
				}
			} else {
				return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
			}
		} catch (Exception e) {
			invokeResult = InvokeResult.error();
			log.error(e.getMessage(), e);
		}
		return invokeResult;
    }
    
    /**
	 * 查找所有 bannerEntity 对象
	 *
	 */
	@RequestMapping(value = "/findAll", method = RequestMethod.GET)
	@ResponseBody
	public InvokeResult<List<BannerEntity>> findAll(int appType) {
		InvokeResult invokeResult = null;
		try {
			List<BannerEntity> bannerEntitys = bannerBusinessService.findAll(appType);
			if (null != bannerEntitys && bannerEntitys.size() > 0) {
				invokeResult = InvokeResult.success(bannerEntitys);
			} else {					
				invokeResult = InvokeResult.pass_no_data();
			}
		} catch (Exception e) {
			invokeResult = InvokeResult.error();
			log.error(e.getMessage(), e);
		}
		return invokeResult;
    }

    /**
     * 新增数据
     *
     */
	@RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
    public InvokeResult<BannerEntity> add(@RequestBody BannerEntity bannerEntity) {
        InvokeResult invokeResult = null;
        try {
			if (null != bannerEntity) {
				invokeResult = InvokeResult.success(bannerBusinessService.insert(bannerEntity));
			} else {
				return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
			}
        } catch (Exception e) {
			invokeResult = InvokeResult.error();
			log.error(e.getMessage(), e);
        }
		return invokeResult;
    }

	/**
	 * 修改数据
	 *
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public InvokeResult<BannerEntity> update(@RequestBody BannerEntity bannerEntity) {
		InvokeResult invokeResult = null;
		try {
			if (null != bannerEntity && null != bannerEntity.getId()) {
				invokeResult = InvokeResult.success(bannerBusinessService.update(bannerEntity));
			} else {
				return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
			}
		} catch (Exception e) {
			invokeResult = InvokeResult.error();
			log.error(e.getMessage(), e);
		}
		return invokeResult;
	}

}